package com.xyz.basesdk.utils;

import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.text.TextUtils;
import android.util.DisplayMetrics;

import com.xyz.basesdk.BuildConfig;
import com.xyz.basesdk.base.BaseApplication;


/**
 * @author Jungly
 * @date 15/3/8 10:07
 */
public class Util {

    public static int dp2px(Context context, int dp) {
        DisplayMetrics displayMetrics = context.getResources()
                .getDisplayMetrics();
        return (int) ((dp * displayMetrics.density) + 0.5);
    }

    /**
     * 把密度转换为像素
     */
    public static int dip2px(float px) {
        final float scale = getScreenDensity(BaseApplication.getContext());
        return (int) (px * scale + 0.5);
    }

    /**
     * 得到设备的密度
     */
    private static float getScreenDensity(Context context) {
        return context.getResources().getDisplayMetrics().density;
    }

    public static void callPhone(String phone) {
        if (TextUtils.isEmpty(phone)) {
            MyToast.showToast("电话号码不能为空");
            return;
        }
        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        BaseApplication.getContext().startActivity(intent);
    }

    /**
     * 通过身份证号截取出生日
     *
     * @param idNumber 身份证号码
     * @return xxxx-xx-xx生日
     */
    public static String getBirthdayFromIDCard(String idNumber) {
        StringBuilder sb = new StringBuilder(10);

        String year = idNumber.substring(6, 10);
        String month = idNumber.substring(10, 12);
        String day = idNumber.substring(12, 14);

        sb.append(year);
        sb.append("-");
        sb.append(month);
        sb.append("-");
        sb.append(day);
        return sb.toString();
    }

    /**
     * 从身份证获取性别
     *
     * @param idNumber 身份证号
     * @return 0女 1男
     */
    public static int getSexFromIFCard(String idNumber) {
        char sexType = idNumber.charAt(idNumber.length() - 2);
        int i = Integer.parseInt(String.valueOf(sexType));
        return i % 2 == 0 ? 0 : 1;
    }

    public static boolean check2K() {
        float hf = getScreenHeight(BaseApplication.getContext());
        return hf > 2300 && hf < 2600;
    }

    /**
     * 得到设备屏幕的高度
     */
    private static int getScreenHeight(Context context) {
        return context.getResources().getDisplayMetrics().heightPixels;
    }

    public static boolean checkS8() {
        float hf = getScreenHeight(BaseApplication.getContext());
        System.out.println("hf=====" + hf);
        return hf >= 2600;
    }

    /**
     * 得到设备屏幕的宽度
     */
    private static int getScreenWidth(Context context) {
        return context.getResources().getDisplayMetrics().widthPixels;
    }

    public static boolean hasConnectedNetwork(Context context) {
        ConnectivityManager connectivity = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo mNetworkInfo = connectivity.getActiveNetworkInfo();
            if (mNetworkInfo != null) {
                return mNetworkInfo.isAvailable();
            }
        }
        return false;
    }

    public static String formatDistance(double distance) {
        if (0.0 == distance) return 0 + "m";
        if (distance > 1000) {
            double d = distance / 1000;
            java.text.DecimalFormat df = new java.text.DecimalFormat("#.0");
            return df.format(d) + "km";
        } else {
            java.text.DecimalFormat df = new java.text.DecimalFormat("#.0");
            return df.format(distance) + "m";
        }
    }

    public static String getVersionStr() {
        return BuildConfig.VERSION_NAME.replace(".", "");
    }
}
